<?php
require("inc/common.inc.php");
/**
  * wechat php test
  */


//define your token
//define("TOKEN", "mxsoft2013wei");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();


class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }

    public function responseMsg()
    {
		global $webdb;
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                global $db;
				//http://www.mxsoft.cn/wx_sample.php?FromUserName=1234&ToUserName=567&MsgType=event&Event=subscribe
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $msgType = trim($postObj->MsgType);
                $keyword = trim($postObj->Content);
				$eventstr = trim($postObj->Event);
                $time = time();
				//判断信息类型
				if ($msgType=="event"){
					if ($eventstr=="subscribe"){
						//判断是否已经存在
						$haveinfo=$db->getinfo("select * from mx_user where weixinid='{$fromUsername}'");
						if (is_array($haveinfo)){
							//更新用户
							$db->excu("update mx_user set isguanzhu='1',guandate='".date("Y-m-d H:i:s")."' where weixinid='{$fromUsername}' and id='{$haveinfo[id]}' and isguanzhu='0' ");
							$userid=$haveinfo[id];
							$resultStr=$this->reg_user($fromUsername,$toUsername);
							exit();
						}
						else{
							//创建用户
							$db->excu("insert into mx_user (regdate,isok,weixinid,isguanzhu,guandate) values('".date("Y-m-d H:i:s")."','1','{$fromUsername}','1','".date("Y-m-d H:i:s")."')");
							//更新用户信息
							wx_update_userinfo("",$fromUsername);
							$resultStr=$this->reg_user($fromUsername,$toUsername);
							exit();
						}
					}
					elseif ($eventstr=="unsubscribe"){
						$db->excu("update mx_user set isguanzhu='0',cguandate='".date("Y-m-d H:i:s")."' where weixinid='{$fromUsername}'");
						exit();
					}
					elseif ($eventstr=="CLICK"){
						$eventkey = intval($postObj->EventKey);
						if ($eventkey>0){
							$huiinfo=$db->getinfo("select * from mx_weixin_menu where id='$eventkey' limit 1");
							if (is_array($huiinfo)){
								if ($huiinfo["mttype"]==4){
								$sucaiinfo=$db->getinfo("select * from mx_sucai_msg where mid='$huiinfo[mtmsgid]' limit 1");
								}
								elseif ($huiinfo["mttype"]!=5){
								$sucaiinfo=$db->getinfo("select * from mx_sucai_file where fid='$huiinfo[mtsucaiid]' limit 1");
								}

								if (is_array($sucaiinfo) or $huiinfo["mttype"]==5){
									if ($huiinfo["mttype"]==1){
										//音乐
										$this->send_wx_music($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpath"]),get_photourl($sucaiinfo["fpath"]));
									}
									elseif ($huiinfo["mttype"]==2){
										//图片
										$this->send_wx_pic($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpath"]));
									}
									elseif ($huiinfo["mttype"]==3){
										//视频
										$this->send_wx_voice($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpic"]),$webdb["weburl"]."suicaishow.php?id=".$sucaiinfo["fid"]);
									}
									elseif ($huiinfo["mttype"]==4){
										//图文
										$scinfo=$db->getinfo("select * from mx_sucai_file where fid='$sucaiinfo[mpic]' limit 1");
										$sucaiinfo["mpic"]=get_photourl($scinfo["fpath"]);
										$newinfo[]=$sucaiinfo;
										if ($sucaiinfo["mtype"]==1){
											$rs2=$db->excu("select * from mx_sucai_msg where fid='$sucaiinfo[mid]' order by addtime asc,mid asc");
											if ($db->num_rows($rs2)>0){
											while ($rsdb2=$db->fetch_array($rs2)){
												$scinfo=$db->getinfo("select * from mx_sucai_file where fid='$rsdb2[mpic]' limit 1");
												$rsdb2["mpic"]=get_photourl($scinfo["fpath"]);
												$newinfo[]=$rsdb2;
											}
											}
										}
										$this->send_wx_new($fromUsername,$toUsername,$newinfo);
									}
									else{
										$this->send_wx_wenzi($fromUsername,$toUsername,$huiinfo["mtcontent"]);
										exit();
									}
								}
							}
						}
					}
					elseif ($eventstr=="LOCATION"){
						//地理位置
						$haveinfo=$db->getinfo("select * from mx_user where weixinid='{$fromUsername}'");
							$weizhix = trim($postObj->Latitude);
							$weizhiy = trim($postObj->Longitude);
							$aa=get_maps_addinfo($weizhix,$weizhiy);
							$province=$aa[province];
							$city=$aa[city];
							$district=$aa[district];
							$address=$aa[address];
							$userid=$haveinfo["id"];
						if (!is_array($haveinfo)){
							//创建用户
							$db->excu("insert into mx_user (regdate,isok,weixinid,weizhix,weizhiy,province,city,district,address,mapupdate,isguanzhu,guandate) values('".date("Y-m-d H:i:s")."','1','{$fromUsername}','$weizhix','$weizhiy','$province','$city','$district','$address','".date("Y-m-d H:i:s")."','1','".date("Y-m-d H:i:s")."')");
							$userid=mysql_insert_id();
							if (is_array($aa)){
							$db->excu("insert into mx_vpp_maplog(userid,weizhix,weizhiy,province,city,district,adddate)value('$userid','$weizhix','$weizhiy','$province','$city','$district','".date("Y-m-d H:i:s")."')");
							}
							$resultStr=$this->reg_user($fromUsername,$toUsername);
							exit();
						}
						if (is_array($aa)){
						$db->excu("update mx_user set weizhix='$weizhix',weizhiy='$weizhiy',province='$province',city='$city',district='$district',address='$address',mapupdate='".date("Y-m-d H:i:s")."' where weixinid='{$fromUsername}'");
						$db->excu("insert into mx_vpp_maplog(userid,weizhix,weizhiy,province,city,district,adddate)values('$userid','$weizhix','$weizhiy','$province','$city','$district','".date("Y-m-d H:i:s")."')");
						}
							//$this->send_wx_wenzi($fromUsername,$toUsername,"感谢您更新自己的地理位置");
						exit();
					}
				}
				elseif ($msgType=="location"){
					//地理位置
					$haveinfo=$db->getinfo("select * from mx_user where weixinid='{$fromUsername}'");
					$weizhix = trim($postObj->Location_X);
					$weizhiy = trim($postObj->Location_Y);
					$aa=get_maps_addinfo($weizhix,$weizhiy);
					$province=$aa[province];
					$city=$aa[city];
					$district=$aa[district];
					$address=$aa[address];
					if (is_array($haveinfo)){
						$db->excu("update mx_user set weizhix='$weizhix',weizhiy='$weizhiy',province='$province',city='$city',district='$district',address='$address',mapupdate='".date("Y-m-d H:i:s")."' where weixinid='{$fromUsername}'");
						$this->send_wx_wenzi($fromUsername,$toUsername,"感谢您更新自己的地理位置");
						exit();
					}
					else{
						//创建用户
						$db->excu("insert into mx_user (regdate,isok,weixinid,weizhix,weizhiy,province,city,district,address,mapupdate,isguanzhu,guandate) values('".date("Y-m-d H:i:s")."','1','{$fromUsername}','$weizhix','$weizhiy','$province','$city','$district','$address','".date("Y-m-d H:i:s")."','1','".date("Y-m-d H:i:s")."')");
						$resultStr=$this->reg_user($fromUsername,$toUsername);
						exit();
					}
				}
				else{
					//判断用户是否注册完成
					$haveinfo=$db->getinfo("select * from mx_user_wreg where user_weixin='{$fromUsername}'");
					if (is_array($haveinfo) and $haveinfo["isfinish"]==0){
						$stepinfo=$db->getinfo("select * from mx_weixin_config where id='$haveinfo[user_step]' limit 1");
						if ($stepinfo["ziduan"]=="mobile"){
							//判断是否输入正确的手机号
							$sjh=trim($postObj->Content);
							if(!eregi("^1[0-9]{10}$",$sjh))
							{
								$this->reg_user($fromUsername,$toUsername);
								exit();
							}
							else{
								$db->excu("update mx_user set username='{$sjh}',password='".md5(substr($sjh,5,6))."',mobile='{$sjh}' where weixinid='{$fromUsername}'");
								$this->reg_user($fromUsername,$toUsername,1);
								exit();
							}
							
						}
						elseif ($stepinfo["ziduan"]=="sex"){
							//判断是否输入性别
							$sex=trim($postObj->Content);
							if($sex!=1 and $sex!=0)
							{
								$this->reg_user($fromUsername,$toUsername);
								exit();
							}
							else{
								$db->excu("update mx_user set sex='{$sex}' where weixinid='{$fromUsername}'");
								$this->reg_user($fromUsername,$toUsername,1);
								exit();
							}
							
						}
						elseif ($stepinfo["ziduan"]=="realname"){
							//判断是否输入姓名
							$realname=trim($postObj->Content);
							if($realname=="")
							{
								$this->reg_user($fromUsername,$toUsername);
								exit();
							}
							else{
								$db->excu("update mx_user set realname='{$realname}' where weixinid='{$fromUsername}'");
								$this->reg_user($fromUsername,$toUsername,1);
								exit();
							}
							
						}
						elseif ($stepinfo["ziduan"]=="shengri"){
							//判断是否输入生日
							$shengri=trim($postObj->Content);
							if($shengri=="")
							{
								$this->reg_user($fromUsername,$toUsername);
								exit();
							}
							else{
								if (strlen($shengri)!=8){
									$this->reg_user($fromUsername,$toUsername);
									exit();
								}
								$shengri=substr($shengri,0,4)."-".substr($shengri,4,2)."-".substr($shengri,6,2);
								if (!isdate($shengri)){
									$this->reg_user($fromUsername,$toUsername);
									exit();
								}
								else{
								$db->excu("update mx_user set shengri='{$shengri}' where weixinid='{$fromUsername}'");
								$this->reg_user($fromUsername,$toUsername,1);
								exit();
								}
							}
							
						}
						elseif ($stepinfo["ziduan"]=="qq"){
							//判断是否输入QQ
							$qq=trim($postObj->Content);
							if(!eregi("^[0-9]",$qq))
							{
								$this->reg_user($fromUsername,$toUsername);
								exit();
							}
							else{
								$db->excu("update mx_user set qq='{$qq}' where weixinid='{$fromUsername}'");
								$this->reg_user($fromUsername,$toUsername,1);
								exit();
							}
							
						}
						elseif ($stepinfo["ziduan"]=="welcome"){
								$this->reg_user($fromUsername,$toUsername);
								exit();
						}
					}
					elseif (!is_array($haveinfo)){
						//创建用户
						$db->excu("insert into mx_user (regdate,isok,weixinid,isguanzhu,guandate) values('".date("Y-m-d H:i:s")."','1','{$fromUsername}','1','".date("Y-m-d H:i:s")."')");
						$resultStr=$this->reg_user($fromUsername,$toUsername);
						exit();
					}
					else{
						//更新用户信息
						wx_update_userinfo("",$fromUsername);
						$vppuserinfo=$db->getinfo("select lastlogintime from mx_user where id='$haveinfo[userid]'");
						if(date("Y-m-d",strtotime($vppuserinfo["lastlogintime"]))!=date("Y-m-d")){
								$db->excu("update mx_user set logindays=logindays+1,logintimes=logintimes+1,lastlogintime='".date("Y-m-d H:i:s")."' where id='$haveinfo[userid]'");
						}
						//微信平台自动回复
						$keyword=trim($keyword);
						if ($keyword!=""){
						    if (wordscut2($keyword,3)=="手机:"){
							//更新手机号
								$sjh=str_replace("手机:","",$keyword);
								if ($sjh==""){
									$this->send_wx_wenzi($fromUsername,$toUsername,"手机号码不能为空");
									exit();
								}
								if(!eregi("^1[0-9]{10}$",$sjh))
								{
									$this->send_wx_wenzi($fromUsername,$toUsername,"手机号码格式错误");
									exit();
								}
								$db->excu("update mx_user set username='{$sjh}',mobile='{$sjh}' where weixinid='{$fromUsername}'");
								$this->send_wx_wenzi($fromUsername,$toUsername,"手机号码修改成功");
								exit();
							}
						    if (wordscut2($keyword,3)=="姓名:"){
							//判断是否输入姓名
								$realname=str_replace("姓名:","",$keyword);
								if($realname=="")
								{
									$this->send_wx_wenzi($fromUsername,$toUsername,"姓名不能为空");
									exit();
								}
								else{
									$db->excu("update mx_user set realname='{$realname}' where weixinid='{$fromUsername}'");
								$this->send_wx_wenzi($fromUsername,$toUsername,"姓名修改成功");
									exit();
								}
							}
						    if (wordscut2($keyword,3)=="生日:"){
							//判断是否输入生日
								$shengri=str_replace("生日:","",$keyword);
								if ($shengri==""){
									$this->send_wx_wenzi($fromUsername,$toUsername,"生日不能为空");
									exit();
								}
								else{
									if (strlen($shengri)!=8){
										$this->send_wx_wenzi($fromUsername,$toUsername,"生日格式错误，正确格式如：19851105");
										exit();
									}
									$shengri=substr($shengri,0,4)."-".substr($shengri,4,2)."-".substr($shengri,6,2);
									if (!isdate($shengri)){
										$this->send_wx_wenzi($fromUsername,$toUsername,"生日格式错误，正确格式如：19851105");
										exit();
									}
									else{
									$db->excu("update mx_user set shengri='{$shengri}' where weixinid='{$fromUsername}'");
									$this->send_wx_wenzi($fromUsername,$toUsername,"生日修改成功");
									exit();
									}
								}
							}
						    if (wordscut2($keyword,3)=="性别:"){
							//判断是否输入性别
								$sex=str_replace("性别:","",$keyword);
								$sex=intval($sex);
								if($sex!=1 and $sex!=0)
								{
									$this->send_wx_wenzi($fromUsername,$toUsername,"性别格式错误。男请回复1，女请回复0");
									exit();
								}
								else{
									$db->excu("update mx_user set sex='{$sex}' where weixinid='{$fromUsername}'");
									$this->send_wx_wenzi($fromUsername,$toUsername,"性别修改成功");
									exit();
								}
							}
						    if (wordscut2($keyword,3)=="QQ:"){
							//判断是否输入QQ
								$qq=str_replace("QQ:","",$keyword);
								if ($qq==""){
									$this->send_wx_wenzi($fromUsername,$toUsername,"QQ号码不能为空");
									exit();
								}
								if(!eregi("^[0-9]",$qq))
								{
									$this->send_wx_wenzi($fromUsername,$toUsername,"QQ号码格式必须为数字");
									exit();
								}
								else{
									$db->excu("update mx_user set qq='{$qq}' where weixinid='{$fromUsername}'");
									$this->send_wx_wenzi($fromUsername,$toUsername,"QQ号码修改成功");
									exit();
								}
							}
							
							//判断有无自动回复
							$huiinfo="";
							$isgetok=0;
							$rs=$db->excu("select * from mx_tuisong_reply where rtitle like '%$keyword%' order by addtime desc,rid desc");
							if ($db->num_rows($rs)>0){
								while ($rsdb=$db->fetch_array($rs)){
									if ($huiinfo==""){$huiinfo=$rsdb;$isgetok=1;}
									else{
										if (trim($rsdb["rtitle"])!=""){
											$rtitle=explode(",",$rsdb["rtitle"]);
											foreach ($rtitle as $key =>$value){
												if (trim($value)==trim($keyword)){
													$huiinfo=$rsdb;
													$isgetok=1;
													break;
												}
											}
											if ($isgetok==1){break;}
										}
									}
								}
							}
							print_r($keyword);
							
							if ($isgetok==0){
								$huiinfo=$db->getinfo("select * from mx_tuisong_reply where rtitle='机器人学习中' order by addtime desc,rid desc");
							}
							
								if ($huiinfo["rtype"]==4){
								$sucaiinfo=$db->getinfo("select * from mx_sucai_msg where mid='$huiinfo[rmsgid]' limit 1");
								}
								elseif ($huiinfo["rtype"]!=5){
								$sucaiinfo=$db->getinfo("select * from mx_sucai_file where fid='$huiinfo[rsucaiid]' limit 1");
								}
								
								$files=array(
									"rid"         =>$huiinfo["rid"],
									"userid"      =>$haveinfo["userid"],
									"rquestion"   =>$keyword,
									"addtime"     =>mktime(),
								);
								$db->exe_insert("mx_tuisong_reply_log",$files);
								
								if (is_array($sucaiinfo) or $huiinfo["rtype"]==5){
									if ($huiinfo["rtype"]==1){
										//音乐
										if ($isgetok==0){
										$sucaiinfo["fname"]="机器人正在努力学习中，请登录微平台获取更多资料";
										}
										$this->send_wx_music($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpath"]),get_photourl($sucaiinfo["fpath"]));
									}
									elseif ($huiinfo["rtype"]==2){
										//图片
										if ($isgetok==0){
										$sucaiinfo["fname"]="机器人正在努力学习中，请登录微平台获取更多资料";
										}
										$this->send_wx_pic($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpath"]));
									}
									elseif ($huiinfo["rtype"]==3){
										//视频
										if ($isgetok==0){
										$sucaiinfo["fname"]="机器人正在努力学习中，请登录微平台获取更多资料";
										}
										$this->send_wx_voice($fromUsername,$toUsername,$sucaiinfo["fname"],$sucaiinfo["fintro"],get_photourl($sucaiinfo["fpic"]),$webdb["weburl"]."suicaishow.php?id=".$sucaiinfo["fid"]);
									}
									elseif ($huiinfo["rtype"]==4){
										//图文
										$scinfo=$db->getinfo("select * from mx_sucai_file where fid='$sucaiinfo[mpic]' limit 1");
										$sucaiinfo["mpic"]=get_photourl($scinfo["fpath"]);
										if ($isgetok==0){
										$sucaiinfo["mtitle"]="机器人正在努力学习中，请登录微平台获取更多资料";
										}
										$newinfo[]=$sucaiinfo;
										if ($sucaiinfo["mtype"]==1){
											$rs2=$db->excu("select * from mx_sucai_msg where fid='$sucaiinfo[mid]' order by addtime asc,mid asc");
											if ($db->num_rows($rs2)>0){
											while ($rsdb2=$db->fetch_array($rs2)){
												$scinfo=$db->getinfo("select * from mx_sucai_file where fid='$rsdb2[mpic]' limit 1");
												$rsdb2["mpic"]=get_photourl($scinfo["fpath"]);
												$newinfo[]=$rsdb2;
											}
											}
										}
										$this->send_wx_new($fromUsername,$toUsername,$newinfo);
									}
									else{
										$this->send_wx_wenzi($fromUsername,$toUsername,$huiinfo["rcontent"]);
										exit();
									}
								}
							
						}
					}
					
//					$textTpl = "<xml>
//								<ToUserName><![CDATA[%s]]></ToUserName>
//								<FromUserName><![CDATA[%s]]></FromUserName>
//								<CreateTime>%s</CreateTime>
//								<MsgType><![CDATA[%s]]></MsgType>
//								<Content><![CDATA[%s]]></Content>
//								<FuncFlag>0</FuncFlag>
//								</xml>"; 
//					if(!empty( $keyword ))
//					{
//						$msgType = "text";
//						$contentStr = $msgType.":".$eventstr.":asdf";
//						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//						echo $resultStr;
//					}else{
//						echo "Input something...";
//					}
				}

        }else {
        	echo "";
        	exit;
        }
    }
    public function reg_user($weixinid,$fromUsername,$setnext=0)
    {
		$setnext=intval($setnext);
		//更新用户信息
		wx_update_userinfo("",$weixinid);
		if ($weixinid!="" and $fromUsername!=""){
			global $db,$webdb;
			$userinfo=$db->getinfo("select * from mx_user where weixinid='$weixinid'");
			if (is_array($userinfo)){
				if ($userinfo["isfinish"]==0){
				//未完成注册
				$wreginfo=$db->getinfo("select * from mx_user_wreg where user_weixin='$weixinid' limit 1");
				if (is_array($wreginfo)){
					$user_step=$wreginfo["user_step"];
					$stepinfo=$db->getinfo("select * from mx_weixin_config where id='$wreginfo[user_step]' and isok='1' limit 1");
					//判断是否是发送欢迎词
					if ($stepinfo["id"]==1 or $setnext==1){
						if($wreginfo["isfinish"]==1){
							$textTpl = "<xml>
									 <ToUserName><![CDATA[%s]]></ToUserName>
									 <FromUserName><![CDATA[%s]]></FromUserName>
									 <CreateTime>%s</CreateTime>
									 <MsgType><![CDATA[news]]></MsgType>
									 <ArticleCount>1</ArticleCount>
									 <Articles>
									 <item>
									 <Title><![CDATA[%s]]></Title> 
									 <Description><![CDATA[%s]]></Description>
									 <PicUrl><![CDATA[%s]]></PicUrl>
									 <Url><![CDATA[%s]]></Url>
									 </item>
									 </Articles>
									 </xml> "; 
							$resultStr = sprintf($textTpl, $weixinid, $fromUsername, time(), "{$webdb[vpp_jigoucode]}欢迎您成为本站会员",$webdb["description"],get_photourl($webdb["weixinlogo"]),$webdb["weburl"]."?wxmxopenid=".$weixinid);
							echo $resultStr;
							exit();
						}
						else{
						$stepnextinfo=$db->getinfo("select * from mx_weixin_config where isok='1' and orderid>'{$stepinfo[orderid]}' order by orderid asc,id asc limit 1");
						if (is_array($stepnextinfo)){
							//更新用户状态
							$db->excu("update mx_user_wreg set user_step='$stepnextinfo[id]',user_setdate='".date("Y-m-d H:i:s")."' where user_weixin='{$weixinid}' and user_step='$stepinfo[id]'");
						}
						//判断是否是完成上一步
						if ($setnext==1){
							$stepinfo=$stepnextinfo;
						}
						}
					}
				}
				else{
					$stepinfo=$db->getinfo("select * from mx_weixin_config where isok='1' order by orderid asc,id asc limit 1");
					$db->excu("insert into mx_user_wreg (userid,user_weixin,user_step,user_setdate)values('{$userinfo[id]}','$weixinid','1','".date("Y-m-d H:i:s")."')");
					
					$user_step=$stepinfo["user_step"];
					//判断是否是发送欢迎词
					if ($stepinfo["id"]==1){
						$stepnextinfo=$db->getinfo("select * from mx_weixin_config where isok='1' and orderid>'{$stepinfo[orderid]}' order by orderid asc,id asc limit 1");
						//更新用户状态
						$db->excu("update mx_user_wreg set user_step='$stepnextinfo[id]',user_setdate='".date("Y-m-d H:i:s")."' where user_weixin='{$weixinid}' and user_step='$stepinfo[id]'");
					}
				}
				//判断是否只有关键词
				//if ($stepinfo["id"]==1){
					$rs=$db->excu("select * from mx_weixin_config where isok='1' and id<>'1' order by id asc limit 1");
					if ($db->num_rows($rs)==0){
						$stepinfo="";	
					}
				//}
				if  (is_array($stepinfo)){
					//提示注册信息
					$msgstr=$stepinfo["content"];
					$textTpl = "<xml>
								<ToUserName><![CDATA[%s]]></ToUserName>
								<FromUserName><![CDATA[%s]]></FromUserName>
								<CreateTime>%s</CreateTime>
								<MsgType><![CDATA[%s]]></MsgType>
								<Content><![CDATA[%s]]></Content>
								<FuncFlag>0</FuncFlag>
								</xml>"; 
					$resultStr = sprintf($textTpl, $weixinid, $fromUsername, time(), "text", $msgstr);
					echo $resultStr;
					exit();
				}
				else{
					//更新用户状态
					$db->excu("update mx_user_wreg set isfinish='1' where user_weixin='$weixinid' and isfinish='0'");
					$textTpl = "<xml>
							 <ToUserName><![CDATA[%s]]></ToUserName>
							 <FromUserName><![CDATA[%s]]></FromUserName>
							 <CreateTime>%s</CreateTime>
							 <MsgType><![CDATA[news]]></MsgType>
							 <ArticleCount>1</ArticleCount>
							 <Articles>
							 <item>
							 <Title><![CDATA[%s]]></Title> 
							 <Description><![CDATA[%s]]></Description>
							 <PicUrl><![CDATA[%s]]></PicUrl>
							 <Url><![CDATA[%s]]></Url>
							 </item>
							 </Articles>
							 </xml> "; 
                	$resultStr = sprintf($textTpl, $weixinid, $fromUsername, time(), "{$webdb[vpp_jigoucode]}欢迎您成为本站会员",$webdb["description"],get_photourl($webdb["weixinlogo"]),$webdb["weburl"]."?wxmxopenid=".$weixinid);
					echo $resultStr;
					exit();
				}
				}
			}
		}
	}
	
	//回复文字
	public function send_wx_wenzi($tostr,$fromstr,$msgstr){
		if ($tostr!="" and $fromstr!="" and $msgstr!=""){
			$textTpl = "<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[text]]></MsgType>
				<Content><![CDATA[%s]]></Content>
				</xml>"; 
				$resultStr = sprintf($textTpl, $tostr, $fromstr, time(),$msgstr);
				echo $resultStr;
				wx_update_userinfo("",$tostr);
				exit();
		}
	}
	
	//回复音乐
	public function send_wx_music($tostr,$fromstr,$title,$des,$musicstr,$musicstrb){
		if ($tostr!="" and $fromstr!="" and $musicstr!=""){
			$textTpl = "<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[music]]></MsgType>
				<Music>
				<Title><![CDATA[%s]]></Title>
 				<Description><![CDATA[%s]]></Description>
				<MusicUrl><![CDATA[%s]]></MusicUrl>
				<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
				</Music>
				</xml>"; 
				$resultStr = sprintf($textTpl, $tostr, $fromstr, time(),$title,$des,$musicstr,$musicstrb);
				echo $resultStr;
				wx_update_userinfo("",$tostr);
				exit();
		}
	}
	//回复视频
	public function send_wx_voice($tostr,$fromstr,$voicetitle,$voicedes,$voicepic,$voicestr){
		if ($tostr!="" and $fromstr!="" and $voicestr!=""){
			$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[news]]></MsgType>
						<ArticleCount>1</ArticleCount>
						<Articles>
						<item>
						<Title><![CDATA[%s]]></Title> 
						<Description><![CDATA[%s]]></Description>
						<PicUrl><![CDATA[%s]]></PicUrl>
						<Url><![CDATA[%s]]></Url>
						</item>
						</Articles>
						 </xml>"; 
				$resultStr = sprintf($textTpl, $tostr, $fromstr, time(),$voicetitle,$voicedes,$voicepic,$voicestr);
				echo $resultStr;
				wx_update_userinfo("",$tostr);
				exit();
		}
	}
	//回复图片
	public function send_wx_pic($tostr,$fromstr,$pictitle,$picdes,$picstr,$picurl){
		if (trim($picurl)==""){$picurl=$picstr;}
		if ($tostr!="" and $fromstr!="" and $picstr!=""){
			$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[news]]></MsgType>
						<ArticleCount>1</ArticleCount>
						<Articles>
						<item>
						<Title><![CDATA[%s]]></Title> 
						<Description><![CDATA[%s]]></Description>
						<PicUrl><![CDATA[%s]]></PicUrl>
						<Url><![CDATA[%s]]></Url>
						</item>
						</Articles>
						 </xml>"; 
				$resultStr = sprintf($textTpl, $tostr, $fromstr, time(),$pictitle,$picdes,$picstr,$picurl);
				echo $resultStr;
				wx_update_userinfo("",$tostr);
				exit();
		}
	}
	//回复图文
	public function send_wx_new($tostr,$fromstr,$new){
		if ($tostr!="" and $fromstr!="" and is_array($new)){
			$textTpl = "<xml>
						<ToUserName><![CDATA[{$tostr}]]></ToUserName>
						<FromUserName><![CDATA[{$fromstr}]]></FromUserName>
						<CreateTime>".time()."</CreateTime>
						<MsgType><![CDATA[news]]></MsgType>
						<ArticleCount>".count($new)."</ArticleCount>
						<Articles>";
			foreach ($new as $key =>$value){
			if (strpos($value[murl],'?')){$value[murl].="&wxmxopenid=".$tostr;}
			else{$value[murl].="?wxmxopenid=".$tostr;}
			$textTpl .= "<item>
						<Title><![CDATA[{$value[mtitle]}]]></Title> 
						<Description><![CDATA[".wordscut(strip_tags($value[mabstract]),100)."]]></Description>
						<PicUrl><![CDATA[{$value[mpic]}]]></PicUrl>
						<Url><![CDATA[{$value[murl]}]]></Url>
						</item>";	
			}			
				$textTpl .= "</Articles>
						 </xml>"; 
				//$resultStr = sprintf($textTpl, $tostr, $fromstr, time(),count($new));
				echo $textTpl;
				wx_update_userinfo("",$tostr);
				exit();
		}
	}
	
	
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
}

?>